﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZX.HIS.Common;
namespace ZX.HIS.UI.Controllers
{
    public class DepartInfoController : Controller
    {
        //
        // GET: /DepartInfo/
        ZX.HIS.BLL.DepartInfo DIBLL = new BLL.DepartInfo();
        /// <summary>
        /// 添加视图
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult DepartInfolAdd()
        {
            // int id =Convert.ToInt32( Session["HospitalId"]);
            ZX.HIS.Model.DepartInfo model = new Model.DepartInfo();
            model.HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    model.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            model.DepartName = Request["DepartName"].ToString();
            int i = DIBLL.DepartInfolAdd(model);
            if (i > 0)
            {
                return Content("<script>alert('添加成功');location.href='/DepartInfo/DepartInfoShow';</script>");
            }
            else
            {
                return Content("<script>alert('添加失败');location.href='/DepartInfo/DepartInfoShow';</script>");
            }
        }
        /// <summary>
        /// 分页显示
        /// </summary>
        /// <param name="PageIndex"></param>
        /// <param name="PageSize"></param>
        /// <param name="StrOrder"></param>
        /// <param name="StrWhere"></param>
        /// <returns></returns>

        public ActionResult DepartInfoShow()
        {
            ZX.HIS.BLL.DepartInfo DIBLL = new BLL.DepartInfo();
            int HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            string name = "";
            Session["DepartName"] = null;
            if (!String.IsNullOrEmpty(Request["DepartName"]))
            {
                name = Request["DepartName"].ToString();
                Session["DepartName"] = name;
            }
            PageClass<ZX.HIS.Model.DepartInfo> pagelist = DIBLL.DepartInfoShow(1, 10, name, HospitalId);
            return View(pagelist);
        }
        public ActionResult page(int id)
        {
            string name = "";
            int HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            if (Session["DepartName"] != null)
            {
                name = Session["DepartName"].ToString();
            }
            PageClass<ZX.HIS.Model.DepartInfo> pagelist = DIBLL.DepartInfoShow(id, 10, name, HospitalId);
            return View("DepartInfoShow", pagelist);
        }

        /// <summary>
        /// 伪删除
        /// </summary>
        /// <param name="IDS"></param>
        /// <param name="DeparStarte"></param>
        /// <returns></returns>
        public ActionResult DepartInfoDelById(int id)
        {
            ZX.HIS.Model.DepartInfo mode = new Model.DepartInfo();
            mode.DepartId = id;
            mode.DepartState = -1;
            ZX.HIS.BLL.DepartInfo DIBLL = new BLL.DepartInfo();
            if (DIBLL.DepartInfoDelById(mode) > 0)
            {
                return Content("<script>alert('删除成功');location.href='/DepartInfo/DepartInfoShow';</script>");
            }
            else
            {
                return Content("<script>alert('删除失败');location.href='/DepartInfo/DepartInfoShow';</script>");
            }
        }
        /// <summary>
        /// 修改视图
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult DepartInfoUpd(int id)
        {
            int HospitalId = 0;
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            if (myCookie != null)
            {
                if (!String.IsNullOrEmpty(myCookie["HospitalId"]))
                {
                    HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
                }
            }
            ZX.HIS.Model.DepartInfo tt = new Model.DepartInfo();
            PageClass<ZX.HIS.Model.DepartInfo> tlist = DIBLL.DepartInfoShow(1, 3, "", HospitalId);
            tt = tlist.PageList.Where(p => p.DepartId == id).FirstOrDefault();
            ViewData["DepartId"] = tt;
            return View("DepartInfoUpd");

        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="DepartName"></param>
        /// <param name="DepartId"></param>
        /// <returns></returns>
        public ActionResult DepartInfoUpdmo(ZX.HIS.Model.DepartInfo mode)
        {
            int i = DIBLL.DepartInfoUpd(mode);
            if (i > 0)
            {

                return Content("<script>alert('修改成功');location.href='/DepartInfo/DepartInfoShow'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败');location.href='/DepartInfo/DepartInfoShow';</script>");
            }
        }

        /// <summary>
        /// Ajax调用所属部门
        /// </summary>
        /// <returns></returns>
        public JsonResult FnDepartInfoList()
        {
            int HospitalId = 0;
            ZX.HIS.Model.UserInfo uModel = new ZX.HIS.Model.UserInfo();
            HttpCookie myCookie = Request.Cookies["MyCookie"];
            uModel.HospitalId = Convert.ToInt32(myCookie["HospitalId"]);
            //uModel.UserPwd = myCookie["UserName"];
            //uModel.ChineseName = HttpUtility.UrlDecode(myCookie["ChineseName"]);
            if (Convert.ToInt32(uModel.HospitalId) != 0)
            {
                HospitalId = Convert.ToInt32(uModel.HospitalId);
            }
            ZX.HIS.BLL.DepartInfo DIBLL = new BLL.DepartInfo();
            return Json(DIBLL.DepartInfoList(HospitalId));
        }

    }
}
